using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
using LitJson;

public class JsonManager
{
    private static JsonManager instance = new JsonManager();
    public static JsonManager Instance => instance;
    private JsonManager() { }

    public void SavaDataToJson<T>(object data,string fileName)
    {
        string path = Application.persistentDataPath + "/" + fileName + ".json";
        string jsonStr = "";

        jsonStr = JsonMapper.ToJson(data);

        File.WriteAllText(path, jsonStr);
    }

    public T LoadDataFromJson<T>(string fileName) where T :new()
    {
        string path = Application.streamingAssetsPath + "/" + fileName + ".json";
        if (!File.Exists(path))
        {
            path = Application.persistentDataPath + "/" + fileName + ".json";
            if (!File.Exists(path))
            {
                return new T();
            }
        }

        string jsonStr = File.ReadAllText(path);
        return JsonMapper.ToObject<T>(jsonStr);
    }
}
